package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.CarCheckVO;
import com.x.erp.rpc.CarCheckRpcService;
import com.x.erp.util.ExcelUtils;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆审核信息
 */
@RestController
@RequestMapping("/carCheck")
public class CarCheckController extends BaseController{

	@Reference
	private CarCheckRpcService carCheckRpcService;//车辆审核信息服务

	/**
	 * 查询车辆审核信息
	 * @param carCheckVO
	 * @return
	 */
	@RequestMapping(value = "/selectCarCheckList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectCarCheckList(CarCheckVO carCheckVO) {
		CarCheckVO carCheck = getEndTime(carCheckVO);
		List<CarCheckVO> carChecks = carCheckRpcService.selectCarCheckList(carCheck);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carChecks);
	}

	/**
	 * 根据条件查询车辆审核数量
	 * @param carCheckVO
	 * @return
	 */
	@RequestMapping(value = "/selectCarCheckNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectCarCheckNum(CarCheckVO carCheckVO) {
		CarCheckVO carCheck = getEndTime(carCheckVO);
		Map<String, Object> res = new HashMap<>();
		res.put("total", carCheckRpcService.selectCarCheckNum(carCheck));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 导出审核EXCEL列表
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/exportCarCheckExcel", method = {RequestMethod.POST, RequestMethod.GET})
	public void exportCarCheckExcel(HttpServletResponse response, CarCheckVO carCheckVO){
		try {
			CarCheckVO carCheck = getEndTime(carCheckVO);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String date = sdf.format(this.getDate(0));
			Workbook workBook = getCarCheckWorkbook(carCheck);
			ExcelUtils.writeExcel(response, workBook, "SHLB_" + date);
		} catch (IOException e) {
			logger.error("导出车辆审核列表失败" + e);
		}
	}

	/**
	 * 获取车辆审核列表工作簿方法
	 * @return
	 */
	private Workbook getCarCheckWorkbook(CarCheckVO carCheckVO){
		List<CarCheckVO> carChecks = carCheckRpcService.selectCarCheckList(carCheckVO);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String[] titles = new String[]{"车辆名称","车架号","城市","市场","车商","车辆来源","车辆状态","检测状态","首次申请时间","审核结果","审核人","审核时间","驳回理由"};
			Workbook workBook = ExcelUtils.initTitle("sheet1", titles);
			for(int i = 0; i < carChecks.size(); i++) {
				Sheet s = workBook.getSheet("sheet1");
				Row row = s.createRow(i + 1);
				int cellNum = 0;
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getBrandName() + carChecks.get(i).getSeriesName() + carChecks.get(i).getModelName());
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getCarVin());
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getCityName());
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getMarketName());
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getShopName());
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getUserShopId() == null?"市场":"车商");
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getCarStockState() == 40?"已出库":carChecks.get(i).getCarStockState() == 30?"出库申请中":carChecks.get(i).getCarSaleState() == 30?"已下架":carChecks.get(i).getCarSaleState() == 20?"已上架":carChecks.get(i).getCarStockState() == 20?"已入库":carChecks.get(i).getCarStockState() == 10?"入库中":"未上架");
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getCarDetectState() == 500?"未初检":carChecks.get(i).getCarDetectState() == 510?"初检不通过":carChecks.get(i).getCarDetectState() == 520?"初检通过":carChecks.get(i).getCarDetectState() == 530?"申请复检中":"复检完成");
				if(carChecks.get(i).getCarLogCreateTimeFirst() != null){
					row.createCell(cellNum++).setCellValue(formatter.format(carChecks.get(i).getCarLogCreateTimeFirst().getTime()));
				}else {
					row.createCell(cellNum++).setCellValue("");
				}
				if(carChecks.get(i).getCarStateValueLast() != null){
					row.createCell(cellNum++).setCellValue(carChecks.get(i).getCarStateValueLast() == 1?"不通过":carChecks.get(i).getCarStateValueLast() == 20?"通过":"未审核");
				}else {
					row.createCell(cellNum++).setCellValue("未审核");
				}
				row.createCell(cellNum++).setCellValue(carChecks.get(i).getUserNameLast());
				if(carChecks.get(i).getCarLogCreateTimeLast() != null){
					row.createCell(cellNum++).setCellValue(formatter.format(carChecks.get(i).getCarLogCreateTimeLast().getTime()));
				}else {
					row.createCell(cellNum++).setCellValue("");
				}
				if(carChecks.get(i).getCarStateValueLast() != null && carChecks.get(i).getCarStateValueLast() == 1){
					row.createCell(cellNum++).setCellValue(carChecks.get(i).getCarLogInfoLast());
				}else {
					row.createCell(cellNum++).setCellValue("");
				}
			}
			return workBook;
		} catch (IOException e) {
			logger.error("导出车辆审核列表失败" + e);
		}
		return null;
	}

	/**
	 * 处理时间查询条件方法
	 * @param carCheckVO
	 * @return
	 */
	private CarCheckVO getEndTime(CarCheckVO carCheckVO){
		if(carCheckVO.getEndTimeFirst() != null){
			carCheckVO.setEndTimeFirst(this.getDate(carCheckVO.getEndTimeFirst(),1));
		}
		if(carCheckVO.getEndTimeLast() != null){
			carCheckVO.setEndTimeLast(this.getDate(carCheckVO.getEndTimeLast(),1));
		}
		return carCheckVO;
	}

}
